import {useEffect, useRef} from "react";
import styles from "./index.less";
import AMapLoader from "@amap/amap-jsapi-loader";

const MapContainer = ({onMapLoaded}) => {
  const map = useRef(null);
  useEffect(() => {
    
    window._AMapSecurityConfig = {
      securityJsCode: "5a2d9430d9f9647abbbe781d8bcbe1c2",
    };
    AMapLoader.load({
      key: '3d7ac83ff39b6116d3b163d56ebe0e18', // 申请好的Web端开发者Key，首次调用 load 时必填
      version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
      plugins: ["AMap.Scale","AMap.MouseTool", "AMap.RectangleEditor", "AMap.PolylineEditor"], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
    }).then((AMap) => {
      map.current = new AMap.Map("mapContainer", {
        // 设置地图容器id
        viewMode: "3D", // 是否为3D地图模式
        zoom: 12, // 初始化地图级别
        center: [116.397428, 39.90923], // 初始化地图中心点位置
      });
      map.current.on('complete', () => {
        onMapLoaded(map.current, AMap)
      });
    }).catch((e) => {
      console.log(e);
    });

    return () => {
      map.current?.destroy();
    };
  }, []);

  return (
    <div
      id="mapContainer"
      className={styles.mapContainer}
    ></div>
  );
}
export default MapContainer;
